home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyPlayAsyncSound.p < prev    next >
Encoding:
Text File  |  1994-09-07  |  1.8 KB  |  100 lines  |  [TEXT/PJMM]

  1. unit MyPlayAsyncSound;
  2.  
  3. interface
  4.  
  5.     procedure InitPlayAsyncSound;
  6.     procedure FinishPlayAsyncSound;
  7.     procedure PlayAsyncSound (theSound: Handle);
  8.     procedure PlayAsyncSoundID (id: integer);
  9.     procedure IdleAsyncSound;
  10.  
  11. implementation
  12.  
  13.     uses
  14.         Sound;
  15.  
  16.     var
  17.         playing: boolean;
  18.         finished: boolean;
  19.         sound: handle;
  20.         chan: SndChannelPtr;
  21.  
  22. {$PUSH}
  23. {$D-}
  24. { Called at interupt level! }
  25.     procedure ChanCallBack (chan: SndChannelPtr; cmd: SndCommand);
  26.         var
  27.             p: ^boolean;
  28.     begin
  29.         p := POINTER(cmd.param2);
  30.         p^ := true;
  31.     end;
  32. {$POP}
  33.  
  34.     procedure FinishSound;
  35.         var
  36.             oe: OSErr;
  37.     begin
  38.         HUnlock(sound);
  39.         playing := false;
  40.         finished := false;
  41.         oe := SndDisposeChannel(chan, false);
  42.         chan := nil;
  43.     end;
  44.  
  45.     procedure PlayAsyncSound (theSound: Handle);
  46.         var
  47.             oe: OSErr;
  48.             myWish: SndCommand;
  49.     begin
  50.         if (theSound <> nil) & (theSound^ <> nil) & not playing then begin
  51.             chan^.qLength := stdQLength;
  52.             oe := SndNewChannel(chan, 0, 0, @ChanCallBack);
  53.             if oe = noErr then begin
  54.                 playing := true;
  55.                 HLock(theSound);
  56.                 oe := SndPlay(chan, theSound, true);
  57.  
  58.                 with myWish do   { set up a sound mgr command block }
  59.                     begin
  60.                     cmd := callBackCmd;  { set playing to false }
  61.                     param1 := 0;
  62.                     param2 := ord(@finished);
  63.                 end; {with}
  64.                 oe := SndDoCommand(chan, myWish, false);
  65. { If any of these commands return with an error, we aren't going to get anywhere anyway }
  66.             end;
  67.         end;
  68.     end;
  69.  
  70.     procedure PlayAsyncSoundID (id: integer);
  71.         var
  72.             sound: handle;
  73.     begin
  74.         if not playing then begin
  75.             sound := GetResource('snd ', id);
  76.             PlayAsyncSound(sound);
  77.         end;
  78.     end;
  79.  
  80.     procedure InitPlayAsyncSound;
  81.     begin
  82.         playing := false;
  83.         finished := false;
  84.         sound := nil;
  85.         chan := nil;
  86.     end;
  87.  
  88.     procedure FinishPlayAsyncSound;
  89.     begin
  90.         if playing then
  91.             FinishSound;
  92.     end;
  93.  
  94.     procedure IdleAsyncSound;
  95.     begin
  96.         if finished then
  97.             FinishSound;
  98.     end;
  99.  
  100. end.